#include "Refractor.h"


Refractor::Refractor() : tex_refractions(0), rtv_refractions(0), srv_refractions(0), tex_refractions_depth(0), dsv_refractions_depth(0)
{
	tex_refractions_depth = 0;
	dsv_refractions_depth = 0;
}


Refractor::~Refractor()
{
	try
	{
		if (srv_refractions)
			srv_refractions->Release();
		
		if (tex_refractions)
			tex_refractions->Release();
		
		if (rtv_refractions)
			rtv_refractions->Release();
		
		if (tex_refractions_depth)
			tex_refractions_depth->Release();
		
		if (dsv_refractions_depth)
			dsv_refractions_depth->Release();

	}
	catch(...)
	{
		MessageBoxA(0, "Exception thrown in Refractor destructor", 0, 0);
	}
}



HRESULT Refractor::createRefractionTexture2D(ID3D11Device* g_dev, const D3D11_TEXTURE2D_DESC& desc)
{
	return g_dev->CreateTexture2D(&desc, 0, &tex_refractions);
}

HRESULT Refractor::createDepthTexture2D(ID3D11Device* g_dev, const D3D11_TEXTURE2D_DESC& desc)
{
	return g_dev->CreateTexture2D(&desc, 0, &tex_refractions_depth);
}

HRESULT Refractor::createRenderTargetView(ID3D11Device* g_dev, const D3D11_RENDER_TARGET_VIEW_DESC& rtv_desc)
{
	return g_dev->CreateRenderTargetView(tex_refractions, &rtv_desc, &rtv_refractions);
}

HRESULT Refractor::createShaderResourceView(ID3D11Device* g_dev, const D3D11_SHADER_RESOURCE_VIEW_DESC& srv_desc)
{
	return g_dev->CreateShaderResourceView(tex_refractions, &srv_desc, &srv_refractions);
}

HRESULT Refractor::createDepthStencilView(ID3D11Device* g_dev, const D3D11_DEPTH_STENCIL_VIEW_DESC& dsv_desc)
{
	return g_dev->CreateDepthStencilView(tex_refractions_depth, &dsv_desc, &dsv_refractions_depth);
}

void Refractor::setRenderTarget(ID3D11DeviceContext* g_context) const
{
	ID3D11RenderTargetView* rtv[1] = { rtv_refractions };

	float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
	g_context->ClearRenderTargetView(rtv_refractions, black);
	g_context->ClearDepthStencilView(dsv_refractions_depth, D3D11_CLEAR_DEPTH, 1.0f, 0xff);
	g_context->OMSetRenderTargets(1, rtv, dsv_refractions_depth); 

}

void Refractor::passRefractionSRV(ID3DX11EffectShaderResourceVariable** fx_srv)
{
	(*fx_srv)->SetResource(this->srv_refractions);
}